import 'package:flutter/material.dart';

class TweenSequenceDemo extends StatefulWidget {
  const TweenSequenceDemo({super.key});
  static const String routeName = 'basics/chaining_tweens';

  @override
  State<TweenSequenceDemo> createState() => _TweenSequenceDemoState();
}

class _TweenSequenceDemoState extends State<TweenSequenceDemo>
    with SingleTickerProviderStateMixin {
  static const Duration _duration = Duration(seconds: 3);
  late final AnimationController _controller;
  late final Animation<Color?> animation;

  static final colors = [
    Colors.red,
    Colors.orange,
    Colors.yellow,
    Colors.blue,
    Colors.indigo,
    Colors.purple,
  ];

  @override
  void initState() {
    super.initState();
    final sequeenceItems = <TweenSequenceItem<Color?>>[];
    for (var i = 0; i < colors.length; i++) {
      final beginColor = colors[i];
      final endColor = colors[(i + 1) % colors.length];
      final weight = 1 / colors.length;

      sequeenceItems.add(
        TweenSequenceItem<Color?>(
          tween: ColorTween(begin: beginColor, end: endColor),
          weight: weight,
        ),
      );
    }

    _controller = AnimationController(duration: _duration, vsync: this);
    animation = TweenSequence<Color?>(sequeenceItems).animate(_controller);
  }

  @override
  void dispose() {
    super.dispose();
    _controller.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Tween Sequences')),
      body: Center(
        child: AnimatedBuilder(
          animation: animation,
          builder: (context, child) {
            return MaterialButton(
              onPressed: () async{// TODO: 此处为何要使用 async/await ？
               await _controller.forward();
                _controller.reset();
              },
              color: animation.value,
              child: child,
            );
          },
          child: Text('Animate', style: TextStyle(color: Colors.white),),
        ),
      ),
    );
  }
}
